// pages/myVideo/myVideo.js
var app = getApp();
var api = require('../../utils/api.js');
import Dialog from '../../miniprogram_npm/@vant/weapp/dialog/dialog';
Page({
  // 设置分享到朋友圈的标题
  onShareTimeline:function(){
    return {
      title:"智克艺术 - 我的视频",
      query:""
    }
  },
  /**
   * 页面的初始数据
   */
  data: {
    myVideo:[],//已发布的视频
    mybVideo:[],//已购买的视频
    TabCur:0,
    tabs:[{
      id: '1',
      cate_name: '我发布的视频'
    },{
      id: '2',
      cate_name: '我购买的视频'
    }]
  },
  toVideo(e) {
    if(this.data.TabCur==0){
      let index = e.currentTarget.dataset.id
      let file_id = this.data.myVideo[index].file_id
      wx.navigateTo({
        url: '../videoDetail/videoDetail?file_id='+file_id
      })
    }else{
      let index = e.currentTarget.dataset.id
      let file_id = this.data.mybVideo[index].file_id
      wx.navigateTo({
        url: '../videoDetail/videoDetail?file_id='+file_id
      })
    }
    
  },
  // 组件调用删除视频
  delete(){
    let token = wx.getStorageSync('token')
    let file_id = this.data.file_id
    const that = this
    wx.request({
      url: app.globalData.host+'VideoToken/delete_video',
      method:'POST',
      data:{
        token,
        file_id
      },
      success(res){
        if(res.data.code==2000){
          wx.showToast({
            title: res.data.msg,
            icon:'none'
          })
        }else{
          wx.showToast({
            title: res.data.msg,
            icon:'none'
          })
        }
        that.getMyVideo(1)
      },
      fail(err){
        console.log(err)
      }
    })
  },
  showdialog(e){
    let id = e.currentTarget.dataset.file_id
    this.setData({
      // show:true,
      file_id:id
    })
    Dialog.confirm({
      title: '警告',
      zIndex:9999,
      message: '此操作不可撤销!请谨慎操作',
    })
      .then(() => {
        this.delete()
      })
      .catch(() => {
        wx.showToast({
          title: '已取消操作',
        })
      });
    
  },
  onClose(){
    this.setData({
      show:false
    })
  },
  // 切换tab
  tabSelect(e) {
    if (e.currentTarget.dataset.idx == this.data.TabCur) {
      return
    }
    let cate_id = e.currentTarget.dataset.id
    console.log(cate_id)
    this.setData({
      TabCur: e.currentTarget.dataset.idx,
      scrollLeft: (e.currentTarget.dataset.idx - 1) * 90,
      cate_id
    })
    if (this.data.code == '2000' && cate_id == 2) {
      let params = {
        store_id: this.data.store_id,
        token: wx.getStorageSync('token'),
        page: this.data.page,
        search: this.data.keywords
      }
      let that = this
      this.agent(that, params)
    }
    if (this.data.code == '2000' && cate_id == 3) {
      let params = {
        store_id: this.data.store_id,
        token: wx.getStorageSync('token'),
        page: this.data.page,
      }
      let that = this
      this.getBalance(that, params)
    }
  },
  getBoughtVideo(pages){
    const that = this
    let token = wx.getStorageSync('token')
    api.fetchApi('Video/my_list',{token}).then(res=>{
        let mybVideo = that.data.mybVideo
        let mybVideo1 = res
        if (pages == 1) {
          mybVideo = res
        } else {
          mybVideo = [...mybVideo, ...mybVideo1]
        }
        for (var index in mybVideo)
        that.setData({
          mybVideo,
          pages,
          files_id:mybVideo[index].file_id,
          pageNums:mybVideo1.length,
        })
    })
  },
  getMyVideo(page){
    const that = this
    let token = wx.getStorageSync('token')
    wx.request({
      url: app.globalData.host+'Video/my_send_video',
      data:{token},
      method:'GET',
      success(res){
        if(res.data.data==''){
          wx.showToast({
            title: '没有视频了',
            icon:'none'
          })
          that.setData({
            myVideo:[]
          })
          return
        }
        let myVideo = that.data.myVideo
        let myVideo1 = res.data.data
        if (page == 1) {
          myVideo = res.data.data
        } else {
          myVideo = [...myVideo, ...myVideo1]
        }
        for (var index in myVideo)
        that.setData({
          myVideo,
          page,
          pageNum:myVideo1.length,
        })
      }
    })
  },
  // 上传视频
  upload(){
    wx.navigateTo({
      url: '/pages/upload/upload?upVideo='+this.data.upVideo,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getMyVideo(1)
    this.getBoughtVideo(1)
    this.setData({
      upVideo:options.upVideo
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var page = this.data.page || 1;
    var pages = this.data.pages || 1;
    page++;
    pages++
    if(this.data.TabCur==0){
      if (this.data.pageNum < 10) {
        wx.showToast({
          title: '没有更多视频了',
          icon: 'none',
        })
        return
      }
      this.getMyVideo(page);
    }else{
      if (this.data.pageNums < 10) {
        wx.showToast({
          title: '没有更多视频了',
          icon: 'none',
        })
        return
      }
      this.getBoughtVideo(pages)
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})